package gameEngine.agents.choosingStrategy.representation;


import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import auxiliary.AuxiliaryClass;
import Data.ConfigurationParameters;


/**
 * @author vladimir && alon
 *	The Class is logicly represent ManuallAdaptive strategy
 *	The perpos of the class:
 *		Be container of data in ExperimentProperties
 *		Functionality for build and open XML data of ManuallAdaptive strategy 
 */
public class ManuallAdaptiveRepresentation extends StrategyRepresentation{

	Logger log = Logger.getLogger(this.getClass());
	
	public ArrayList<StrategyRepresentation> strategies = new ArrayList<StrategyRepresentation>();
	public ArrayList<Integer> strategiesTurn = new ArrayList<Integer>();

	public ManuallAdaptiveRepresentation(String name)
	{
		this.strategyName = name;
	}
	
	public void addStrategy(StrategyRepresentation strategy, int numberturns)
	{
		strategies.add(strategy);
		strategiesTurn.add(numberturns);
	}
	
	
	@Override
	public void setExtraElement(Document doc, Element strategy) {
		Element strategiesElement = doc.createElement("Strategies");
		
		log.debug("Number of Strategies is " + strategies.size());
		strategy.appendChild(strategiesElement);
		//go over the strategies and add them to the XML
		for(int i = 0; i < strategies.size(); ++i)
		{
			//strategyRepresentation.getXMLElement(doc);
			Element nextStrategy = strategies.get(i).getXMLElement(doc);
			//the turns element will be after the strategy
			Element turns = doc.createElement("Turns");
			turns.appendChild(doc.createTextNode
					(Integer.toString(strategiesTurn.get(i))));
			strategiesElement.appendChild(nextStrategy);
			strategiesElement.appendChild(turns);
		}	
	}
	
	@Override
	public void openExtraStrategyData(Document doc, Element strategyElement,
			StrategyRepresentation strategyRepresentation) {
		NodeList listOfStrategies = strategyElement.getElementsByTagName(ConfigurationParameters.Strategy.STRATEGY);
		NodeList listOfTurns = strategyElement.getElementsByTagName("Turns");
		
		
		for(int i = 0; i < listOfStrategies.getLength(); ++i)
		{
			//take strategy element
			Element innerStrategyElement = (Element)listOfStrategies.item(i);
			//create/take strategy object from the XML
			StrategyRepresentation stRepresentation = openXMLElement(doc, innerStrategyElement);
			//add to the list
			strategies.add(stRepresentation);
			
			//add number of turns to this strategy
			Element turnsElement = (Element)listOfTurns.item(i);
			//there just one
			Node numNode = turnsElement.getChildNodes().item(0);
			String numStr = numNode.getNodeValue();
			if(AuxiliaryClass.isNumber(numStr))
			{
				int num = Integer.parseInt(numStr);
				strategiesTurn.add(num);
			}
		}
		
	}
	
	public int numberOfStrategies()
	{
		return strategies.size();
	}
	
	public StrategyRepresentation getStrategy(int ndx)
	{
		return strategies.get(ndx - 1);
	}
	
	public int getNumberTurns(int ndx)
	{
		return strategiesTurn.get(ndx - 1);
	}

	public ArrayList<StrategyRepresentation> getStrategies() {
		return strategies;
	}

	public ArrayList<Integer> getStrategiesTurn() {
		return strategiesTurn;
	}
	
}
